<template>
  <transition :name="animateName">
    <div id="MyLoading" class="loader" v-show="isShow">
      <div class="loader-inner pacman">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  name: 'Loading',
  data () {
    return {
      isShow: false,
      hasAnimate: true,
      onHiding: null
    }
  },
  created () {
    var _this = this
    alphaHub.$on('display', function () {
      clearTimeout(this.onHiding)
      _this.isShow = true
      this.onHiding = setTimeout(function () {
        _this.isShow = false
      }, 10000)
    })
    alphaHub.$on('hide', function () {
      _this.isShow = false
      clearTimeout(this.onHiding)
    })
  },
  computed: {
    /**
     * 动画效果样式，没有返回空
     * @return {String} 样式
     */
    animateName () {
      return this.hasAnimate ? 'opacity' : ''
    }
  },
  methods: {
    /**
     * 开启动画效果
     */
    opemAnimate () {
      this.hasAnimate = true
    },
    /**
     * 去除动画效果
     * @return {Promise} 返回promise
     */
    removeAnimate () {
      return new Promise(resolve => {
        this.hasAnimate = false
        resolve()
      })
    }
  },
  destroyed: function () {
    alphaHub.$off('display')
    alphaHub.$off('hide')
  }
}
</script>

<style lang="scss" scope>
@import "../sass/variables.scss";
.loader {
  position: fixed;
  z-index: 1112;
  display: flex;
  flex: 0 1 auto;
  flex-direction: column;
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: 25%;
  width: 100%;
  align-items: center;
  justify-content: center;
  height: 100vh;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.3);
}

.pacman {
  position: relative;
}
.pacman > div:nth-child(2) {
  -webkit-animation: pacman-balls 1s 0s infinite linear;
  animation: pacman-balls 1s 0s infinite linear;
}
.pacman > div:nth-child(3) {
  -webkit-animation: pacman-balls 1s 0.22s infinite linear;
  animation: pacman-balls 1s 0.22s infinite linear;
}
.pacman > div:nth-child(4) {
  -webkit-animation: pacman-balls 1s 0.44s infinite linear;
  animation: pacman-balls 1s 0.44s infinite linear;
}
.pacman > div:nth-child(5) {
  -webkit-animation: pacman-balls 1s 0.66s infinite linear;
  animation: pacman-balls 1s 0.66s infinite linear;
}
.pacman > div:first-of-type {
  width: 0px;
  height: 0px;
  border-right: 25px solid transparent;
  border-top: 25px solid $primary;
  border-left: 25px solid $primary;
  border-bottom: 25px solid $primary;
  border-radius: 25px;
  -webkit-animation: rotate_pacman_half_up 0.5s 0s infinite;
  animation: rotate_pacman_half_up 0.5s 0s infinite;
}
.pacman > div:nth-child(2) {
  width: 0px;
  height: 0px;
  border-right: 25px solid transparent;
  border-top: 25px solid $primary;
  border-left: 25px solid $primary;
  border-bottom: 25px solid $primary;
  border-radius: 25px;
  -webkit-animation: rotate_pacman_half_down 0.5s 0s infinite;
  animation: rotate_pacman_half_down 0.5s 0s infinite;
  margin-top: -50px;
}
.pacman > div:nth-child(3),
.pacman > div:nth-child(4),
.pacman > div:nth-child(5),
.pacman > div:nth-child(6) {
  background-color: $primary;
  width: 15px;
  height: 15px;
  border-radius: 100%;
  margin: 2px;
  width: 10px;
  height: 10px;
  position: absolute;
  -webkit-transform: translate(0, -6.25px);
  -ms-transform: translate(0, -6.25px);
  transform: translate(0, -6.25px);
  top: 25px;
  left: 100px;
}

@-webkit-keyframes rotate_pacman_half_up {
  0% {
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }

  50% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }

  100% {
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }
}

@keyframes rotate_pacman_half_up {
  0% {
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }

  50% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }

  100% {
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }
}

@-webkit-keyframes rotate_pacman_half_down {
  0% {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }

  50% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }
}

@keyframes rotate_pacman_half_down {
  0% {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }

  50% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }
}

@-webkit-keyframes pacman-balls {
  75% {
    opacity: 0.7;
  }

  100% {
    -webkit-transform: translate(-100px, -6.25px);
    transform: translate(-100px, -6.25px);
  }
}

@keyframes pacman-balls {
  75% {
    opacity: 0.7;
  }

  100% {
    -webkit-transform: translate(-100px, -6.25px);
    transform: translate(-100px, -6.25px);
  }
}
</style>
